<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Wechat;
use app\common\model\Banner;
use app\common\model\Cart;
use app\common\model\Category;
use app\common\model\Goods;
use app\common\model\Order;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['index','category','goodsList'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
     
        //小程序LOGO
        $logo = config('site.site_logo') ? cdnurl(config('site.site_logo'), true) : '';
        $site_name = config('site.site_name');
        $site_name_en = config('site.site_name_en');
        $site_banner = Banner::all();
        $category = Category::where('type', 'index')->select();
        $this->success('请求成功', compact('logo', 'category', 'site_name', 'site_name_en', 'site_banner'));
    }
    /**
     * 分类
     *
     */
    public function category()
    {
        // $list = collection(Category::all(['type' => 'default'], ['goods']))->toArray();
        $user_id = $this->auth->id;
        $list  = collection(Category::with(['goods' => function ($q) use ($user_id) {
            $q->with(['cart' => function ($qq) use ($user_id) {
                $qq->where('user_id', $user_id);
            }]);
        }])->where('type', 'default')->select())->toArray();
        $goods = Goods::all(['state' => 2],['cart' => function ($qq) use ($user_id) {
            $qq->where('user_id', $user_id);
        }]);
        // foreach ($goods as &$vo) {
        //     $vo['cart_num'] = Cart::where('user_id', $user_id)->where('goods_id', $vo['id'])->value('num') ?? 0;
        // }
        $array = [
            'id' => 0,
            'name' => '推荐',
            'goods' => $goods
        ];
        array_unshift($list, $array);
        $this->success('请求成功', $list);
    }

    /**
     *商品列表
     *
     */
    public function goodsList()
    {
        $get = request()->get();
        extract($get);
        if (!isset($category_id)) {
            $this->error('参数错误');
        }
        if ($category_id == 0) {
            $where['state'] = '2';
        } else {
            $where['category_id'] = $category_id;
            $where['state'] = ['<>', '0'];
        }
        $list = Goods::where($where)->paginate(10);
        $this->success('请求成功', $list);
    }


    /**
     * 添加购物车
     *
     */
    public function add_cart()
    {
        $param = $this->request->post();
        if ((!isset($param['goods_id']) && empty($param['goods_id']))) {
            $this->error('参数错误');
        }
        if (!isset($param['num']) || $param['num'] < 0) {
            $this->error('添加的数量有误');
        }
        $param['uid'] = $this->auth->id;
        $res = Cart::addCart($param);
        if ($res)
            $this->success('成功');
        $this->error('失败');
    }
    /**
     * 删除购物车
     *
     */
    public function del_cart()
    {
        $param = $this->request->post();
        $res = Cart::delCart($this->auth->id);
        if ($res)
            $this->success('删除成功');
        $this->error('删除失败');
    }
    /**
     * 查看购物车
     *
     */
    public function cart_info()
    {
        $uid = $this->auth->id;
        $data = Cart::list($uid);
        $cloumn = array_column($data, 'num');
        $this->success('获取成功', ['data' => $data, 'count' => array_sum($cloumn)]);
    }
}
